package com.sz.myinterface;

import com.aliyun.api.gateway.demo.util.HttpUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import java.util.HashMap;
import java.util.Map;

public class Weather {

    public static String getWeather(String city){

        String host = "https://jisutqybmf.market.alicloudapi.com";
        String path = "/weather/query";
        String method = "GET";//GET/POST 任意
        String appcode = "045ca0303c534d55bec9100912040eb5";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/json; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("city", city);
//        querys.put("citycode", "citycode");
//        querys.put("cityid", "cityid");
//        querys.put("ip", "ip");
//        querys.put("location", "location");

        String str="";
        try {
            HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
            System.out.println(response.toString());
            //获取response的body
            str=EntityUtils.toString(response.getEntity(),"utf-8");
            System.out.println(str);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return str;
    }

    public static void main(String[] args) {

        String ci = getWeather("深圳");

        System.out.println("=== = " + ci);
    }

}
